package com.zhengdongzulin.zuke.app.main.apartment

import androidx.lifecycle.rxLifeScope
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.app.data.BookHouseInfo
import com.zhengdongzulin.zuke.base.BaseViewModel
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class ReserveVm : BaseViewModel() {

    var reservation_apply: StateLiveData<String> = StateLiveData()

    fun reservation_apply(
        name: String?,
        phone: String?,
        seeTime: String?,
        note: String?,
        houseId: String = "",
        roomTypeId: String = ""
    ) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.RESERVATION_APPLY)
                    .add("name", name)
                    .add("phone", phone)
                    .add("seeTime", seeTime)
                    .add("note", note)
                    .add("houseId", houseId)
                    .add("roomTypeId", roomTypeId)
                    .toResponse<String>()
                    .await()
                reservation_apply.postValueAndSuccess(r)
            }, {
                assignment(it, reservation_apply)
            }
        )
    }

    var apply_book_info_res: StateLiveData<BookHouseInfo> = StateLiveData()
    fun applyBookInfo(houseId: String = "") {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.APPLY_BOOK_INFO)
                    .add("houseId", houseId)
                    .toResponse<BookHouseInfo>()
                    .await()
                apply_book_info_res.postValueAndSuccess(r)
            }, {
                assignment(it, reservation_apply)
            }
        )
    }

    fun book_pay_now(
        houseShouDingId: String = "",
        houseId: String = "",
        type: String = "",
        zukeName: String = "",
        zukePhone: String = "",
        signDate: String = "",
        remark: String = "",
        money: String = ""
    ) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.BOOK_PAY_NOW)
                    .add("houseShouDingId", houseShouDingId)
                    .add("houseId", houseId)
                    .add("type", type)
                    .add("zukeName", zukeName)
                    .add("zukePhone", zukePhone)
                    .add("signDate", signDate)
                    .add("remark", remark)
                    .add("money", money)
                    .toResponse<BookHouseInfo>()
                    .await()
                //apply_book_info_res.postValueAndSuccess(r)
            }, {
                assignment(it, reservation_apply)
            }
        )
    }
}